suppressMessages(library(MASS))

setwd(out_path)
if(!dir.exists("PartySupport")){
	dir.create("PartySupport")
}else{
	warning("Overwriting previous run, if any")
}
setwd("PartySupport")

###################
#Party support/opposition, aggregate
###################

#Rep support (ordered)
rep.opinion.op <- polr(factor(rep) ~ pred50_sw + pred90_sw, 
	data = aic_clean,method="probit")
rep.full.ctls.op <-  polr(factor(rep) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + econ.policies + fp.policies, 
	data = aic_clean,method="probit")
rep.full.op <-  polr(factor(rep) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + econ.policies + fp.policies +
	b.scaled.intgrp + m.scaled.intgrp, 
	data = aic_clean,method="probit")

#Dem support (ordered)
dem.opinion.op <- polr(factor(dem) ~ pred50_sw + pred90_sw, 
	data = aic_clean,method="probit")
dem.full.ctls.op <-  polr(factor(dem) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + econ.policies + fp.policies, 
	data = aic_clean,method="probit")
dem.full.op <-  polr(factor(dem) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + econ.policies + fp.policies +
	b.scaled.intgrp + m.scaled.intgrp, 
	data = aic_clean,method="probit")

###################
#Party support/opposition by issue area
###################

#Rep support (ordered), FP
fp.rep.opinion.op <- polr(factor(rep) ~ pred50_sw + pred90_sw, 
	data = fp.policies,method="probit")
fp.rep.full.op <-  polr(factor(rep) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = fp.policies,method="probit")

#Rep support (ordered), EC
ec.rep.opinion.op <- polr(factor(rep) ~ pred50_sw + pred90_sw, 
	data = econ.policies,method="probit")
ec.rep.full.op <-  polr(factor(rep) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = econ.policies,method="probit")

#Rep support (ordered), NE
ne.rep.opinion.op <- polr(factor(rep) ~ pred50_sw + pred90_sw, 
	data = ne.policies,method="probit")
ne.rep.full.op <-  polr(factor(rep) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = ne.policies,method="probit")

#Dem support (ordered), FP
fp.dem.opinion.op <- polr(factor(dem) ~ pred50_sw + pred90_sw, 
	data = fp.policies,method="probit")
fp.dem.full.op <-  polr(factor(dem) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = fp.policies,method="probit")

#Dem support (ordered), EC
ec.dem.opinion.op <- polr(factor(dem) ~ pred50_sw + pred90_sw, 
	data = econ.policies,method="probit")
ec.dem.full.op <-  polr(factor(dem) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = econ.policies,method="probit")

#Dem support (ordered), FP
ne.dem.opinion.op <- polr(factor(dem) ~ pred50_sw + pred90_sw, 
	data = ne.policies,method="probit")
ne.dem.full.op <-  polr(factor(dem) ~ pred50_sw + pred90_sw + 
	rescaled_ideol_code + 
	b.scaled.intgrp + m.scaled.intgrp, 
	data = ne.policies,method="probit")

##################
# Interest group pref
##################
#Business group interests
bus.opinion <- lm(b.scaled.intgrp ~ pred50_sw + pred90_sw, data = aic_clean)
bus.full <- lm(b.scaled.intgrp ~ pred50_sw + pred90_sw + rescaled_ideol_code + econ.policies + fp.policies, data = aic_clean)

#Advocacy group interests
adv.opinion <- lm(m.scaled.intgrp ~ pred50_sw + pred90_sw, data = aic_clean)
adv.full <- lm(m.scaled.intgrp ~ pred50_sw + pred90_sw + rescaled_ideol_code + econ.policies + fp.policies, data = aic_clean)

##################
# Model tables
##################

#Republican support table (aggregate) (ordered probit)
texreg(list(rep.opinion.op, rep.full.ctls.op, rep.full.op),
		file="1_op_REPsup.tex",
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", 
			"Ideology" , "Economic Policies" , "Foreign Policies" ,
			"Business Preferences" , "Advocacy Group Preferences"),
		caption = "Republican Support and Opposition to Proposed Policies (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("Opinion", "Opinion+Ctls", "Full"))

	  # Republican (FP) (ordered probit)
	texreg(list(fp.rep.opinion.op, fp.rep.full.op),
		file="1_op_REPsupISS_FP.tex",      
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
		caption = "Republican Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("REP-FP-opinion", "REP-FP-Full"))
	
	  # Republican (EC) (ordered probit)
	texreg(list(ec.rep.opinion.op, ec.rep.full.op),file="1_op_REPsupISS_EC.tex",      
	      custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
	          caption = "Republican Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
	          caption.above=T,
	          custom.model.names = c("REP-EC-opinion", "REP-EC-Full"))
	
	  # Republican (NE) (ordered probit)
	texreg(list(ne.rep.opinion.op, ne.rep.full.op),file="1_op_REPsupISS_NE.tex",      
	      custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
	          caption = "Republican Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
	          caption.above=T,
	          custom.model.names = c("REP-NE-opinion", "REP-NE-Full"))

	  # Republican (All Issues) (ordered probit)
	texreg(list(fp.rep.opinion.op, fp.rep.full.op, ec.rep.opinion.op, ec.rep.full.op, ne.rep.opinion.op, ne.rep.full.op),file="1_op_REPsupISS_All.tex",      
	      custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
	          caption = "Ordered Probit Models of Republican Leader Support By Issue Area",
	          caption.above=T,
	          custom.model.names = c("REP-Foreign", "REP-Foreign", "REP-Econ", "REP-Econ", "REP-Social", "REP-Social"))


 #Dem support table (aggregate) (ordered probit)
texreg(list(dem.opinion.op, dem.full.ctls.op, dem.full.op),
		file="1_op_DEMsup.tex",       
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology" , 
			"Economic Policies" , "Foreign Policies", 
			"Business Preferences", "Advocacy Group Preferences"),
		caption = "Democratic Support and Opposition to Proposed Policies (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("Opinion", "Opinion+Ctls", "Full"))

	  # Democratic (FP) (ordered probit)
	texreg(list(fp.dem.opinion.op, fp.dem.full.op),file="1_op_DEMsupISS_FP.tex",      
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
		caption = "Democratic Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("DEM-FP-opinion", "DEM-FP-Full"))
	
	  # Democratic (EC) (ordered probit)
	texreg(list(ec.dem.opinion.op, ec.dem.full.op),file="1_op_DEMsupISS_EC.tex",      
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
		caption = "Democratic Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("DEM-EC-opinion", "DEM-EC-Full"))
	
	  # Democratic (NE) (ordered probit)
	texreg(list(ne.dem.opinion.op, ne.dem.full.op),file="1_op_DEMsupISS_NE.tex",      
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
		caption = "Democratic Support and Opposition to Proposed Policies by Issue Area (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("DEM-NE-opinion", "DEM-NE-Full"))

	  # Democratic (All Issues) (ordered probit)
	texreg(list(fp.dem.opinion.op, fp.dem.full.op, ec.dem.opinion.op, ec.dem.full.op, ne.dem.opinion.op, ne.dem.full.op),file="1_op_DEMsupISS_All.tex",      
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology", "Business Preferences" , "Advocacy Group Preferences"),
		caption = "Ordered Probit Models of Democratic Leader Support By Issue Area",
		caption.above=T,
		custom.model.names = c("DEM-Foreign", "DEM-Foreign", "DEM-Econ", "DEM-Econ", "DEM-Social", "DEM-Social"))

#Aggregate Party Support
texreg(list(rep.opinion.op, rep.full.ctls.op, rep.full.op, dem.opinion.op, dem.full.ctls.op, dem.full.op),
		file="1_op_PARTYsup.tex",       
		custom.coef.names = c("Middle Class Preferences", "Affluent Preferences", "Ideology" , 
			"Economic Policies" , "Foreign Policies", 
			"Business Preferences", "Advocacy Group Preferences"),
		caption = "Democratic Support and Opposition to Proposed Policies (Ordered Probit)",
		caption.above=T,
		custom.model.names = c("REP-Support", "REP-Support", "REP-Support", "DEM-Support","DEM-Support","DEM-Support"))

#Group interests
texreg(list(bus.opinion,bus.full,adv.opinion,adv.full),
		file="1_ols_GRPsup.tex",      
		custom.coef.names = c("(intercept)","Middle Class Preferences", "Affluent Preferences", 
			"Ideology" , "Economic Policies" , "Foreign Policies"),
		caption = "Interest group Support and Opposition to Proposed Policies (Multiple Regression)",
		caption.above=T,
		custom.model.names = c("Bus. grps (Opinion)","Bus. grps (Full)",
			"Adv. grps (Opinion)","Adv. grps (Full)"))

setwd(script_path)